﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ex4.Person
{
    enum PersonGender
    {
        Undefined,
        Male,
        Female
    }
    class Person : IPropertyChangeNotification
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string firstName;
        private string lastName;
        private PersonGender gender;
        private string birthDate;

        public string FirstName
        {
            get { return this.firstName; }
            set
            {
                OnPropertyChanged("FirstName", this.firstName, value);
                this.firstName = value;
            }
        }
        public string LastName
        {
            get { return lastName; }
            set
            {
                if (value != null)
                {
                    OnPropertyChanged("LastName", this.lastName, value);
                }
                this.lastName = value;
            }
        }
        public PersonGender Gender
        {
            get { return this.Gender; }
            set
            {
                OnPropertyChanged("Gender", this.gender, value);
                this.gender = value;
            }
        }
        public string BirthDate
        {
            get { return this.birthDate; }
            set
            {
                OnPropertyChanged("BirthDate", this.birthDate, value);
                this.birthDate = value;
            }
        }

        public Person(string firstName, string lastName, PersonGender gender, string birthDate)
        {
            this.PropertyChanged += new PropertyChangedEventHandler(Person_PropertyChanged);
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Gender = gender;
            this.BirthDate = birthDate;
        }

        //Receiver method (handler) 
        static void Person_PropertyChanged(object sender, PropertyChangedEventArgs eventArgs)
        {
            Console.WriteLine("Property '{0}' is Changed!", eventArgs.NameOfProperty);
            if (eventArgs.OldValue == null)
                Console.WriteLine("Old value is 'NULL'! ");
            else
                Console.WriteLine("Old value is '{0}'!", eventArgs.OldValue);
            Console.WriteLine("New value is '{0}'!\n", eventArgs.NewValue);
        }

        //Fires the event 
        protected void OnPropertyChanged(string propertyName, object oldValue, object newValue)
        {
            if (PropertyChanged != null)
            {
                PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName, oldValue, newValue);
                PropertyChanged(this, args);
            }
        }

        //public override string ToString()
        //{
        //    return string.Format("\nFirstName: {0}",this.FirstName);
        //}

    }
}
